﻿using BMC.ARSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wicresoft.EKA.NRemedy.Linq
{
    public static class LinqHelper
    {
        public static object GetResult<T>(this IQueryable entry, ARLoginContext context) where T : ARBaseForm
        {
            object result = null;

            TranslateResult tr = new QueryVisitor().Translate(Evaluator.PartialEval(entry.Expression));
            if (tr.GroupIds.Count > 0)
            {
                ARProxy<T> proxy = new ARProxy<T>(context);

                switch (tr.StatisticStatusCode)
                {
                    case 1:
                        result = proxy.GetEntryStatistics(tr.Qualification.ToString(), null, 1, tr.GroupIds.ToArray());
                        break;
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                        result = proxy.GetEntryStatistics(tr.Qualification.ToString(), tr.StatisticNoCountExpresion, tr.StatisticStatusCode, tr.GroupIds.ToArray());
                        break;
                    default:
                        throw new NotSupportedException(string.Format("StatisticStatusCode {0} Not Support", tr.StatisticStatusCode.ToString()));
                }
            }
            else if (tr.Count)
            {
                ARProxy<T> proxy = new ARProxy<T>(context);
                result = proxy.GetEntryCountByQuery(tr.Qualification.ToString());
            }
            else if (tr.SelectIds.Count > 0)
            {
                ARProxy<T> proxy = new ARProxy<T>(context);
                if (tr.Skip.HasValue && tr.Take.HasValue && tr.OrderIds.Count > 0)
                {
                    ArrayList sortInfo = new ArrayList();
                    foreach (var item in tr.OrderIds)
                    {
                        if (item > 0)
                        {
                            sortInfo.Add(new SortInfo(item, SortInfo.SortOrder.Ascending));
                        }
                        else
                        {
                            sortInfo.Add(new SortInfo(-item, SortInfo.SortOrder.Descending));
                        }
                    }

                    int number;
                    result = proxy.GetEntryWithStartIndexAndPagesize(tr.Qualification.ToString(), tr.SelectIds, (uint)tr.Skip.Value, (uint)tr.Take.Value, out number, sortInfo);
                }
                else
                {
                    result = proxy.GetListEntry(tr.Qualification.ToString(), tr.SelectIds);
                }
            }
            else
            {
                ARProxy<T> proxy = new ARProxy<T>(context);
                result = proxy.GetListEntry(tr.Qualification.ToString());
            }
            return result;
        }

        public static TranslateResult GetTranslateResult(this IQueryable entry, ARLoginContext context)
        {
            return new QueryVisitor().Translate(Evaluator.PartialEval(entry.Expression));
        }

        public static string GetQualification(this IQueryable entry, ARLoginContext context)
        {
            return "(" + new QueryVisitor().Translate(Evaluator.PartialEval(entry.Expression)).Qualification.ToString() + ")";
        }
    }
}
